package com.add_picture_to_contact;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.res.Resources.NotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.view.KeyEvent;

import java.io.ByteArrayOutputStream;

public class add_picture_to_contact_activity extends Activity {
	private final String num = "1301111666";
	private final String pic_name_part = "c";
	private final String path_head_part = "/sdcard/b";
	private final String path_tail_part = ".jpeg";
	private final int pic_count = 4;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_F) {
			addContact(pic_count);
			return true;
		} else
			return super.onKeyDown(keyCode, event);
	}

	public void addContact(int count) throws NotFoundException, IllegalArgumentException {
		for (int i = 0; i < count; i++) {
			ContentValues values = new ContentValues();
			long rawContactId = 0;
			Uri rawContactUri;
			rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
			rawContactId = ContentUris.parseId(rawContactUri);

			values.put(Data.RAW_CONTACT_ID, rawContactId);
			values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
			values.put(StructuredName.GIVEN_NAME, Integer.toString(i));
			values.put(StructuredName.FAMILY_NAME, "_fname");
			getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

			Uri mobileUri = Uri.withAppendedPath(rawContactUri, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
			values.clear();
			values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
			values.put(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, 1);
			values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
			values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, num + Integer.toString(i));
			getContentResolver().insert(mobileUri, values);

			values.clear();
			Bitmap myselfPhoto = BitmapFactory.decodeFile(path_head_part + Integer.toString(i)+path_tail_part);
			ByteArrayOutputStream bytes = new ByteArrayOutputStream();
			myselfPhoto.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

			int photoRow = -1;
			String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + rawContactId + " AND " + ContactsContract.Data.MIMETYPE + "=='"
					+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
			Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
			int idIdx = cursor.getColumnIndexOrThrow(BaseColumns._ID);
			if (cursor.moveToFirst()) {
				photoRow = cursor.getInt(idIdx);
			}
			cursor.close();
			values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
			values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
			values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes.toByteArray());
			values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);

			if (photoRow >= 0) {
				getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, BaseColumns._ID + " = " + photoRow, null);
			} else {
				getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
			}
		}
	}
}
